package com.hbt.word_consumer.controller;

import com.hbt.api.entity.*;
import com.hbt.word_consumer.service.WordConsumerFeign;
import com.hbt.word_consumer.service.WordUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("word_consumer")
public class WordConsumerController {
    @Autowired
    WordConsumerFeign wordConsumerFeign;
    @Autowired
    WordUserFeign wordUserFeign;

    @PostMapping("del_word")
    public CommonResult<String> delWord(int wordid){
        return wordConsumerFeign.delWord(wordid);
    }

    @PostMapping("get_progress")
    public CommonResult<StaticBean> getProgress(@RequestParam("token") String token){
        CommonResult<User> commonResult = wordUserFeign.getUserByToken(token);
        User user = commonResult.getT();
        return wordConsumerFeign.getProgress(user.getId());
    }

    @PostMapping("find_similar")
    public CommonResult<List<Word>> findSimilar(String src, int rate, String token){
        //根据token获取word list，然后传参数到feign
        CommonResult<User> commonResult = wordUserFeign.getUserByToken(token);
        //初始化WordsCondition
        WordsCondition wordsCondition = new WordsCondition();
        wordsCondition.setFeature_strs(new LinkedList<String>());
        wordsCondition.setUserid(commonResult.getT().getId());
        wordsCondition.setWord("");

        List<Word> words = wordConsumerFeign.getWordByCondition(wordsCondition);
        return wordConsumerFeign.findSimilar(src, rate, words);
    }

    @PostMapping("update_word")
    public CommonResult updateWord(Word word){
        System.out.println("controller word:"+word);
        return wordConsumerFeign.updateWord(word);
    }

    @PostMapping("get_word_by_wordid")
    public CommonResult<Word> getWordByWordid(@RequestParam("wordid") int wordid){
        System.out.println("controller wordid:"+wordid);
        Word word_result = wordConsumerFeign.getWordByWordID(wordid);
        CommonResult result = new CommonResult<Word>();
        result.setT(word_result);
        if (word_result != null){
            result.setCode(200);
            result.setMsg("获取单词成功");
        }else {
            result.setCode(0);
            result.setMsg("获取单词失败");
        }
        return result;
    }

    @RequestMapping("fanyi")
    public CommonResult fanyi(@RequestParam("src") String src){
        System.out.println("enter fanyi controller src:"+src);
        return wordConsumerFeign.fanyi(src);
    }

    @RequestMapping("add_word")
    public CommonResult addWord(Word word){
        System.out.println("word_condition:"+word.toString());
        //先通过token获取userid，并设置到word进去
        CommonResult<User> commonResult = wordUserFeign.getUserByToken(word.getToken());
        word.setUserid(commonResult.getT().getId());

        CommonResult<String> result = wordConsumerFeign.addWord(word);
        return result;
    }

    @RequestMapping("get_word_by_condition")
    public CommonResult<List<Word>> getWordByCondition(WordsCondition wordsCondition){
        System.out.println("word_condition:"+wordsCondition.toString());

        //先通过token获取userid，并设置到word进去
        CommonResult<User> commonResult = wordUserFeign.getUserByToken(wordsCondition.getToken());
        wordsCondition.setUserid(commonResult.getT().getId());
        List<Word> words = wordConsumerFeign.getWordByCondition(wordsCondition);
        for (Word word: words){
            System.out.println("根据条件获取的word："+word);
        }
        CommonResult<List<Word>> result = new CommonResult<List<Word>>();
        result.setT(words);
        result.setCode(200);
        result.setMsg("获取words成功");
        return result;
    }

}
